﻿#include "api_chef.h"

Chef::Chef(QObject *parent) : QObject(parent)
{

}

void Chef::chef_modify_info(QJsonObject &data) {

}

void Chef::chef_modify_info_finished() {
    
}

void Chef::get_chef_info(QString token, int uid) {
    this->jwt_token = token;
    this->uid = uid;

    QString url = "/chef/get/chef/info/";
    QString jwt_token = this->jwt_token;
    QJsonObject data;
    data["uid"] = this->uid;

    this->request = new HttpPostRequest();
    this->request->post(url, jwt_token, data);

    QObject::connect(this->request, &HttpPostRequest::post_finished, this, &Chef::get_chef_info_finished);
}
void Chef::get_chef_info_finished() {
    QJsonObject tmp = this->request->get_return_data().toObject();
    this->id = tmp["id"].toInt();
    this->name = tmp["name"].toString();
    this->phone = tmp["phone"].toString();
    this->sex = tmp["sex"].toString();
//    qDebug() << this->name << endl;
    QObject::disconnect(this->request, &HttpPostRequest::post_finished, this, &Chef::get_chef_info_finished);
}

void Chef::get_all_unfinished() {
    QString url = "/order/item/get/all/unfinished/";
    QString jwt_token = this->jwt_token;
    QJsonObject data;
    data["uid"] = this->uid;

    this->order_item_request = new HttpPostRequest();
    this->order_item_request->post(url, jwt_token, data);

    QObject::connect(this->order_item_request, &HttpPostRequest::post_finished, this, &Chef::get_all_unfinished_finished);
}
void Chef::get_all_unfinished_finished() {
    this->unfinished_order_item_list = this->order_item_request->get_return_data().toArray();
    emit this->get_all_unfinished_signal();
    QObject::disconnect(this->order_item_request, &HttpPostRequest::post_finished, this, &Chef::get_all_unfinished_finished);
}

void Chef::done(int order_item_id) {
    QString url = "/order/item/done/";
    QString jwt_token = this->jwt_token;
    QJsonObject data;
    data["chef_id"] = this->id;
    data["order_item_id"] = order_item_id;

    this->order_item_request = new HttpPostRequest();
    this->order_item_request->post(url, jwt_token, data);

    QObject::connect(this->order_item_request, &HttpPostRequest::post_finished, this, &Chef::done_finished);
}
void Chef::done_finished() {
    emit this->done_signal();
    QObject::disconnect(this->order_item_request, &HttpPostRequest::post_finished, this, &Chef::done_finished);
}

